// GetFileVerInfoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "GetFileVerInfo.h"
#include "GetFileVerInfoDlg.h"
#include "TestDataEditDlg.h"
#include "InsertDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGetFileVerInfoDlg dialog

CGetFileVerInfoDlg::CGetFileVerInfoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGetFileVerInfoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGetFileVerInfoDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGetFileVerInfoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGetFileVerInfoDlg)
	DDX_Control(pDX, IDC_LIST_TESTDATA, m_listTestData);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGetFileVerInfoDlg, CDialog)
	//{{AFX_MSG_MAP(CGetFileVerInfoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BTN_OPENDATAFILE, OnBtnOpendatafile)
	ON_BN_CLICKED(IDC_BTN_SAVEDATAFILE, OnBtnSavedatafile)
	ON_BN_CLICKED(IDC_BTN_EDIT, OnBtnEdit)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_TESTDATA, OnDblclkListTestdata)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGetFileVerInfoDlg message handlers

BOOL CGetFileVerInfoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_listTestData.ModifyStyle(0, LVS_REPORT);
	CRect rc;
	m_listTestData.GetClientRect(&rc);
	m_listTestData.InsertColumn(0, _T("FileName"), LVCFMT_LEFT, rc.Width()/2);
	m_listTestData.InsertColumn(1, _T("FileVersion"), LVCFMT_LEFT, rc.Width()/2);

	//m_listTestData.EnableWindow(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGetFileVerInfoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CGetFileVerInfoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CGetFileVerInfoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CGetFileVerInfoDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CFileDialog *pdlg = NULL;
	pdlg = new CFileDialog(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "DLL Files (*.dll)|*.dll|EXE Files (*.exe)|*.exe|All Files (*.*)|*.*||", NULL);
	if (NULL != pdlg)
	{
		if (IDOK == pdlg->DoModal())
		{
			CString strFileName = pdlg->GetPathName();
			CString strRet = GetVersionFromPath(strFileName);
			MessageBox(strRet);
		}
		delete pdlg;
	}
}

CString CGetFileVerInfoDlg::GetVersionFromPath(CString IDEPath)
{
	DWORD dwLen = 0;
	char* lpData = NULL;

	dwLen = GetFileVersionInfoSize(IDEPath , 0);
	if (0 == dwLen)
	{
		return _T("");
	}
	lpData = new char[dwLen + 1];
	if (NULL != lpData)
	{
		if (!GetFileVersionInfo(IDEPath, 0, dwLen, lpData))
		{
			delete[] lpData;
			return _T("");
		}
		LPVOID lpBuffer = NULL;
		UINT uLen = 0;

		BOOL bSuccess = VerQueryValue(lpData, _T("\\StringFileInfo\\040904b0\\FileVersion"), &lpBuffer, &uLen);
		if (!bSuccess)
		{
			delete lpData;
			return CString("");
		}
		CString Ret = (char*)lpBuffer;
		Ret.Replace(',','.');
		Ret.Replace(' ', '0');
		delete[] lpData;
		return Ret;
	}
	else
	{
		return _T("");
	}
	//
}

void CGetFileVerInfoDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	TCHAR infoBuf[32768];
	if( !GetSystemDirectory( infoBuf, 32768 ) )
	{
		MessageBox("GetSystemDirectory error");
	}
	MessageBox(infoBuf); 
}

void CGetFileVerInfoDlg::OnBtnOpendatafile() 
{
	// TODO: Add your control notification handler code here
	HANDLE hFile = CreateFile(_T("C:\\testdata2.txt"),
		GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ,
		NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

	if (hFile == INVALID_HANDLE_VALUE)
		AfxMessageBox(_T("Couldn't create the file!"));
	else
	{
		// Attach a CFile object to the handle we have.
		CFile myFile((int)hFile);
		
		int length = myFile.GetLength();
		

		//int i = length / 1024;
		char* sz_data = new char[length + 1];
		myFile.Read(sz_data, length);
		sz_data[length] = '\0';
		CString str_data(sz_data);
		//MessageBox(str_data);

		int len = str_data.GetLength();
		int temp = str_data.Find("<testdata>", 0);
		str_data = str_data.Right(len - temp - 12);
		temp = str_data.Find("</testdata>", 0);
		str_data = str_data.Left(temp);
		//MessageBox(str_data);

		while (-1 != str_data.Find("\r\n"))
		{
			temp = str_data.Find("\r\n");
			len = str_data.GetLength();
			CString str_testdata = str_data.Left(temp);
			str_data = str_data.Right(len - temp - 2);
		}
		

		if (length >= 25)
		{
		}

// 		char sz_head[13] = {0};
// 		char head[] = "<testdata>\r\n";
// 		char sz_tail[14] = {0};
// 		myFile.Read(sz_head, 12);
// 		char tail[] = "</testdata>\r\n";
// 		sz_head[12] = '\0';
// 		int cmp = strcmp(sz_head, head);
// 		myFile.Seek(-13, CFile::end);
// 		myFile.Read(sz_tail, 13);
// 		sz_tail[13] = '\0';
// 		cmp = strcmp(sz_tail, tail);

		delete[] sz_data;

		//static const char sz[] = "<testdata>\r\n</testdata>\r\n";
		//myFile.SeekToEnd();
		//myFile.Write(sz, lstrlen(sz));
		
		//lenght = myFile.GetLength();
		

		myFile.Close();
   }
}

void CGetFileVerInfoDlg::OnBtnSavedatafile() 
{
	// TODO: Add your control notification handler code here
	HANDLE hFile = CreateFile(_T("C:\\MyFile1.txt"),
		GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ,
		NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	
	if (hFile == INVALID_HANDLE_VALUE)
		AfxMessageBox(_T("Couldn't create the file!"));
	else
	{
		// Attach a CFile object to the handle we have.
		CFile myFile((int)hFile);

		int lenght = myFile.GetLength();
		
		static const char sz[] = "Hockey is best!\r\n";
		
		// write string, without null-terminator
		myFile.SeekToEnd();
		myFile.Write(sz, lstrlen(sz));

		lenght = myFile.GetLength();
		
		// We can call Close() explicitly, but the destructor would have
		// also closed the file for us. Note that there's no need to
		// call the CloseHandle() on the handle returned by the API because
		// MFC will close it for us.
		myFile.Close();
   }
}

// 
// HRESULT CGetFileVerInfoDlg::GetTestDataFromFile(CFile* pfile, PTESTDATA ptestdata, DWORD* pcount)
// {
// 	ASSERT(pfile);
// 
// 	return 0;
// }

void CGetFileVerInfoDlg::OnBtnEdit() 
{
	// TODO: Add your control notification handler code here
	CTestDataEditDlg dlg;
	dlg.DoModal();
}

void CGetFileVerInfoDlg::OnDblclkListTestdata(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int index = m_listTestData.GetSelectionMark();
	if (-1 == index)
	{
		CInsertDlg dlg;
		if (IDOK == dlg.DoModal())
		{
			
		}
	}
	*pResult = 0;
}
